package com.dodock.android.banglapapers.ui;

import java.util.Vector;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dodock.android.banglapapers.BanglaNews;
import com.dodock.android.banglapapers.ComplexScriptHelper;
import com.dodock.android.banglapapers.MyApplication;
import com.dodock.android.banglapapers.NewsDetails;
import com.dodock.android.banglapapers.R;
import com.dodock.android.banglapapers.Utility;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

public class SavedNewsActivity extends BaseActivity{
	
	private Typeface fontBN;
	private Typeface fontEN;
	
	private Tracker mGaTracker;
	private GoogleAnalytics mGaInstance;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		//Google Analytics
		try{
		    mGaInstance = GoogleAnalytics.getInstance(this);
		    mGaTracker = mGaInstance.getTracker(getResources().getString(R.string.ga_trackingId));
		}
		catch (Exception e) {
		}
		//--Google Analytics
		
				
		setContentView(R.layout.home);			
		fontBN = Typeface.createFromAsset(this.getAssets(), "font/SutonnyMJ.ttf");
		fontEN = Typeface.DEFAULT;
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText(R.string.SavedNews);
		
		ImageButton settingsButton = (ImageButton) findViewById(R.id.settingsBtn);
		settingsButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				settingsActivity();
			}
		});
		
		Button topNewsButton = (Button) findViewById(R.id.topNews);
		topNewsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SavedNewsActivity.this, TopNewsActivity.class);
				startActivity(intent);
			}
		});

		Button favouritButton = (Button) findViewById(R.id.favorite);
		favouritButton.setVisibility(View.INVISIBLE);

		Button savedNewsButton = (Button) findViewById(R.id.savedNews);
		savedNewsButton.setText(R.string.favorites);
		savedNewsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SavedNewsActivity.this, FavouritNewsActivity.class);
				startActivity(intent);
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		com.facebook.Settings.publishInstallAsync(this, MyApplication.fb_app_id);
		setInContentView();
	}
	public static Vector<NewsDetails> newsList = new Vector<NewsDetails>();
	public Vector<NewsDetails> newsDetailsList ;
	
	private void setInContentView() {
		LinearLayout mainContainer = (LinearLayout) findViewById(R.id.homeMainContainer);
		mainContainer.removeAllViews();
		
		if(HomeActivity.newspapersList == null || HomeActivity.newspapersList.size()  < 1){
			return;
		}
		
		for(int i = 0; i < HomeActivity.newspapersList.size(); i++){
			final int paperno = i;
			newsDetailsList = new Vector<NewsDetails>();
			newsDetailsList = BanglaNews.getDatabaseManager().getSavedNewsOfPaper(HomeActivity.newspapersList.elementAt(paperno).getId());			
			if(newsDetailsList.size() > 0 ){
				LinearLayout container = new LinearLayout(this);
				LinearLayout.LayoutParams containerParms = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT, 70);
				containerParms.setMargins(0, 5, 0, 5);
				container.setLayoutParams(containerParms);
				container.setBackgroundColor(Color.WHITE);
				container.setGravity(Gravity.CENTER);
				
				TextView newsPaperTitle = new TextView(this);
				LinearLayout.LayoutParams newsPaperTitleParams = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT, 70);
				newsPaperTitle.setLayoutParams(newsPaperTitleParams);
				newsPaperTitle.setGravity(Gravity.CENTER);
				newsPaperTitle.setTextAppearance(getBaseContext(), android.R.style.TextAppearance_Large);
				newsPaperTitle.setTextColor(Color.BLACK);
				if(HomeActivity.newspapersList.get(paperno).getLanguage().equals("bn")){					
					if(Utility.isOnlyEngCharacters(HomeActivity.newspapersList.elementAt(paperno).getName())){
						newsPaperTitle.setTypeface(fontEN);
						newsPaperTitle.setText(HomeActivity.newspapersList.elementAt(paperno).getName());
					}else{
						newsPaperTitle.setTypeface(fontBN);					
						newsPaperTitle.setText(ComplexScriptHelper.UnicodeToAnsi(HomeActivity.newspapersList.elementAt(paperno).getName()));
					}
				}else{
					newsPaperTitle.setTypeface(fontEN);
					newsPaperTitle.setText(HomeActivity.newspapersList.elementAt(paperno).getName());
				}
				container.addView(newsPaperTitle);
				mainContainer.addView(container);
				for(int j = 0; j < newsDetailsList.size(); j++){
					newsList.add(newsDetailsList.elementAt(j));
					LinearLayout newsTitleLayout = new LinearLayout(this);
					newsTitleLayout.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
					LinearLayout.LayoutParams newsTitleLayoutParms = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, 70);
					newsTitleLayout.setLayoutParams(newsTitleLayoutParms);
					TextView titleView = new TextView(this);
					final int position = j;
					titleView.setOnClickListener(new OnClickListener() {
						String key = newsDetailsList.elementAt(position).getId();
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Intent intent = new Intent(SavedNewsActivity.this, NewsDetailsActivity.class);
							intent.putExtra("newsPaperId", HomeActivity.newspapersList.get(paperno).getId());
							intent.putExtra("language", HomeActivity.newspapersList.get(paperno).getLanguage());
							intent.putExtra("newsId", key);
							intent.putExtra("parent", "SavedNews");
							intent.putExtra("newsNo", position);
							startActivity(intent);
						}
					});
					titleView.setTextAppearance(getBaseContext(), android.R.style.TextAppearance_Large);
					LinearLayout.LayoutParams textViewParms = new LinearLayout.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, 70);
					textViewParms.gravity = Gravity.CENTER;
					textViewParms.setMargins(15, 0, 15, 0);
					titleView.setLayoutParams(textViewParms);
					titleView.setTextColor(Color.BLACK);
					titleView.setGravity(Gravity.CENTER);
					titleView.setPadding(15, 0,0,0);
					
					if(newsDetailsList.size() == 1){
						titleView.setBackgroundResource(R.drawable.round_rectangle_gray);
					}else{
						if(j == 0){
							titleView.setBackgroundResource(R.drawable.roundtopgray);
						}else if( j == newsDetailsList.size() - 1){
							titleView.setBackgroundResource(R.drawable.roundbottomgray);
						}else{
							titleView.setBackgroundResource(R.drawable.rectanglegray);
						}
					}
					if(HomeActivity.newspapersList.get(i).getLanguage().equals("bn")){
						titleView.setTypeface(fontBN);
						titleView.setText(newsDetailsList.elementAt(j).getTitle());
					}else{
						titleView.setTypeface(fontEN);
						titleView.setText(newsDetailsList.elementAt(j).getTitle());
					}
					newsTitleLayout.addView(titleView);
					mainContainer.addView(newsTitleLayout);
				}
			}
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		try {
			mGaTracker.sendView("/SavedNewsList");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
}
